package controller

import (
	"bluebell-backend/common"
	"bluebell-backend/model/po"
	"bluebell-backend/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

func AddPost(c *gin.Context) {
	// 解析参数,由于是token，所以uuid在后端拿
	post := new(po.Post)
	err := c.ShouldBindJSON(post)
	if err != nil {
		fmt.Println("参数解析失败")
		return
	}
	uuid, ok := c.Get(common.CtxUserIDKey)
	if !ok {
		fmt.Println("没有信息")
		common.Error(c, common.CodeNeedLogin)
		return
	}
	post.Uid = uuid.(int64)

	// 逻辑

	code, err := service.AddPost(post)
	if err != nil {
		common.Error(c, common.ERROR)
		return
	}
	common.OK(c, code, nil)
	return
}

func UpdatePost(c *gin.Context) {
	// 参数解析
	post := new(po.Post)
	err := c.ShouldBindJSON(post)
	if err != nil {
		fmt.Println("参数解析失败")
		return
	}
	// 逻辑
	code, err := service.UpdatePost(post)
	if err != nil {
		common.Error(c, common.ERROR)
		return
	}
	common.OK(c, code, nil)
	return
}

func GetPostInfo(c *gin.Context) {
	//
	strId := c.Param("id")
	id, err := strconv.ParseInt(strId, 10, 64)
	if err != nil {
		fmt.Println("参数解析失败")
		return
	}

	post, err := service.GetPostInfo(id)
	if err != nil {
		fmt.Println("查询失败")
		common.Error(c, common.ERROR)
		return
	}
	common.OK(c, common.SUCCESS, post)
	return
}
